<?
require("config.php");
$table="pro_basic";

	$catstr='';
	$cat2='';
if (isset($_GET['cat'])&&$_GET['cat']) {
	$cat=$_GET['cat'];
	$catstr="where category=$cat";
	$cat2=" category=$cat and ";
}

function getName($id) {
	$sql="select name,parent from  pro_category  where id=$id";
	$r=myQuery($sql);
	if ($id) return getName($r[0][1]).'&gt;'.$r[0][0];
		else return'';
}

if ($catstr) {
	$pos=getName($cat);
}
else $pos='All Products';

if (null==$listfield1) $listfield1 = "code";
if (null==$listfield2) $listfield2 = "name";
if (null==$listfield3) $listfield3 = "category";
if (null==$listfield4) $listfield4 = "mark_new";
if (null==$listfield5) $listfield5 = "publish";
if (null==$listfield6) $listfield6 = "brand";
if (null==$listfield7) $listfield7 = "stock";

$extra= "price_euro,auth,weight,issuedate,description,remark,sn_en,sn_cn";


if (null==$ord) $ord=$listfield3;
if (null==$sort) $sort='desc';

if ($sort=='desc') {
	$s='v';
	$as='asc';
}else { 
	$s='^';
	$as='desc';
}

$order=str_replace(',',' '.$sort.',',$ord);
if ($action=="search"){

	$Query = "select id, $listfield1, $listfield2, $listfield3, $listfield4, $listfield5 , $listfield6, $listfield7, $extra from $table WHERE  $cat2 ( $listfield1 Like '%$Search%' OR $listfield2 Like '%$Search%' OR $listfield6 Like '%$Search%')  order by $order $sort, id desc ";

}else{

	$Query = "select id, $listfield1, $listfield2, $listfield3, $listfield4, $listfield5, $listfield6, $listfield7, $extra  from $table $catstr order by $order $sort, id desc";
	
}

	$r=myQuery($Query);



header("Content-type:application/vnd.ms-excel");  
header("Content-Disposition:filename=inv_stock_" . date("Ymd") . ".xls");  



echo $pos."\n\n\n";  
echo "Code\t";  
echo "Product Name (English)\t";
echo "Product Name (Chinese)\t";
echo "Unit Price (HKD)\t";
echo "Current Stock level\t"; 
echo "Product Status\t"; 
echo "Promotion Method\t"; 
echo "Member Discount ?\t"; 
echo "Issue Date\t"; 
echo "Description\t"; 
echo "Special Notes\t"; 
echo "Weight (Kg)\t\n"; 
for ($i=0;$i<count($r);$i++){

echo $r[$i][1] . "\t"; 
echo $r[$i][2] . "\t"; 
echo $r[$i][6] . "\t"; 
echo $r[$i][8] . "\t";
echo $r[$i][7] . "\t"; 
echo ($r[$i][5]?'Display':'Hidden') . "\t"; 
echo ($r[$i][4]?'New Product':'N/A') . "\t"; 
echo ($r[$i][9]?'Yes':'No') . "\t"; 
echo date('Y-m-d',$r[$i][11]) . "\t"; 
echo str_replace("\t"," ",str_replace("\n"," ",str_replace("\r"," ",$r[$i][12].'  '.$r[$i][13]))) . "\t"; 
echo str_replace("\t"," ",str_replace("\n"," ",str_replace("\r"," ",$r[$i][14].'  '.$r[$i][15]))) . "\t"; 
echo $r[$i][10] . "\t\n"; 
}

echo "\n"; 
@mysql_close();
?>